package main

import (
	"fmt"
	"net/http"
)

type HelloHandle struct {}

func (h *HelloHandle) ServeHTTP(w http.ResponseWriter, r *http.Request)  {
	fmt.Fprintf(w, "hello %s", r.URL.Path[2:])
}

/**
 * logPrint接收和返回的都是一个处理器
 * 需要借助http.HandlerFunc将一个匿名函数转换为处理器
 * http.Handler是一个结构体，它绑定了一个ServeHTTP方法，这个方法用来处理HTTP请求
 */
func logPrint(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Println("Handler function called")
		h.ServeHTTP(w, r)
	})
}

func main() {
	hello := HelloHandle{}
	http.Handle("/hello/", logPrint(&hello))

	server := http.Server{
		Addr: "127.0.0.1:8080",
	}
	server.ListenAndServe()
}
